﻿@model NetSurveillance.WebApp.DataServices.Models.IpSetting
<table id="tbIpSetting">
    <tr>
        <td>IP:</td>
        <td>
            @Html.TextBoxFor(m => m.Ip)
            @Html.ValidationMessageFor(m => m.Ip)
        </td>
        <td style="padding-left: 15px;">
            <dl class="filemanage_right_dl">
                <dd id="btnScan">
                    <a href="javascript:;" title="@Resources.Resource.Button_AutoGetIp">
                        @Resources.Resource.Button_AutoGetIp
                    </a>
                </dd>      
            </dl>
        </td>
    </tr>
    <tr>
        <td>@Html.LabelFor(m => m.SubnetMask):</td>
        <td colspan="2">
            @Html.TextBoxFor(m => m.SubnetMask)
            @Html.ValidationMessageFor(m => m.SubnetMask)
        </td>
    </tr>
    <tr>
        <td>@Html.LabelFor(m => m.Gateway):</td>
        <td colspan="2">
            @Html.TextBoxFor(m => m.Gateway)
            @Html.ValidationMessageFor(m => m.Gateway)
        </td>
    </tr>
</table>
@Html.HiddenFor(m => m.Id)
@Html.HiddenFor(m => m.DeviceName)
<script type="text/javascript">
    $(function() {
        $('#btnScan').click(scan);
    });

    function scan() {
        var $btn = $(this), isScanning = $btn.data('isScanning');
        if (!isScanning) {
            $.ajax({
                url: '@Url.Action("StartScan","System")',
                type: 'post',
                dataType: 'json',
                cache: false,
                success: function(result) {
                    if (!result.Success) {
                        alert(result.Message);
                        return;
                    }
                    $btn.data('isScanning', true).find('a').text('@Resources.Resource.Button_Stop');
                    $.showLoading('正在扫描，请等待');
                    getScanResult();
                }
            });
        }
        return false;
    }

    function getScanResult() {
        $.ajax({
            url: '@Url.Action("ScanServerIp","System")',
            type: 'post',
            dataType: 'json',
            cache: false,
            success: function (result) {
                if (!result.Success) {
                    alert(result.Message);
                    $.hideLoading();
                    $('#btnScan').removeData('isScanning').find('a').text('@Resources.Resource.Button_AutoGetIp');
                    return;
                }
                if (result.Success && !result.Expandos) {
                    setTimeout(getScanResult, 2000);
                    return;
                }
                $('#Ip').val(result.Expandos.Ip);
                $('#SubnetMask').val(result.Expandos.SubnetMask);
                $('#Gateway').val(result.Expandos.Gateway);
                $.hideLoading();
                $('#btnScan').removeData('isScanning').find('a').text('@Resources.Resource.Button_AutoGetIp');
            }
        });
    }
</script>